package com.sample.api;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.netflix.astyanax.util.TimeUUIDUtils;
import com.sample.mapping.Caption;
import com.sample.mapping.Comment;
import com.sample.mapping.Follower;
import com.sample.mapping.Friend;
import com.sample.mapping.Picture;
import com.sample.mapping.Post;
import com.sample.mapping.PostCaptionMapping;
import com.sample.mapping.PostCommentsMapping;
import com.sample.mapping.TimeLine;
import com.sample.mapping.User;
import com.sample.mapping.UserLine;
import com.sample.util.ColumnFamilyConfig;

//TODO make pojos rather than passing values in function
public class WebApi {
	
	private Dao dao;
	
	public WebApi(Dao dao) {
		this.dao = dao;
	}
	
	/**
	 * Api to be called for adding new image
	 * @param urlLoc
	 * @param userId
	 * @param postId
	 * @param pictureName
	 * @return
	 */
	public String addImage(String urlLoc,String userId, String postId, String pictureName) {
		String uniqueTimeUUIDinMicros = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
		_addImage(urlLoc, userId, postId, pictureName, uniqueTimeUUIDinMicros);
		return uniqueTimeUUIDinMicros;
	}
	
	

	private void _addImage(String urlLoc, String userId, String postId,
			String pictureName, String uniqueTimeUUIDinMicros) {
		Picture<String> picture = new Picture<String>();
		picture.setId(uniqueTimeUUIDinMicros);
		picture.setPictureName(pictureName);
		picture.setPostId(postId);
		picture.setUserId(userId);
		picture.setUrlLocation(urlLoc);
		picture.setDeleted(false);
		dao.insert(ColumnFamilyConfig.PICTURE.toString(),picture);
	}
	
	/**
	 * Api to be called for adding new post
	 * @param userId
	 * @param data
	 * @param stripId
	 * @return
	 */
	public String addUser(User<String> user){
		String uniqueTimeUUIDinMicros = null;
		if(user != null){
			uniqueTimeUUIDinMicros = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
			user.setUserId(uniqueTimeUUIDinMicros);
			dao.insert(ColumnFamilyConfig.USER.toString(),user);
		}
		return uniqueTimeUUIDinMicros;
	}
	
	public String addPost(String userId, String data) {
		Post<String> post = new Post<String>();
		String uniqueTimeUUIDinMicros = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
		post.setId(uniqueTimeUUIDinMicros);
		post.setUserId(userId);
		post.setDeleted(false);
		post.setContentId(data);
		dao.insert(ColumnFamilyConfig.POST.toString(),post);
		return uniqueTimeUUIDinMicros;
	}
	
	public String addComments(Comment<String> comment){
		String uniqueTimeUUIDinMicros = null;
		if(comment != null){
			uniqueTimeUUIDinMicros = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
			comment.setId(uniqueTimeUUIDinMicros);
			dao.insert(ColumnFamilyConfig.COMMENT.toString(),comment);
		}
		return uniqueTimeUUIDinMicros;
			
	}
	
	public String addCaptions(Caption<String> caption){
		String uniqueTimeUUIDinMicros = null;
		if(caption != null){
			uniqueTimeUUIDinMicros = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
			caption.setId(uniqueTimeUUIDinMicros);
			dao.insert(ColumnFamilyConfig.CAPTION.toString(),caption);
		}
		return uniqueTimeUUIDinMicros;
			
	}

	public void addUserlineUpdate(String userId, String postId) {
		addUserlineUpdate(userId, postId , String.valueOf(System.nanoTime()));
	}
	
	public void addUserlineUpdate(String userId, String postId, String postTime) {
		UserLine<String> line = new UserLine<String>();
		line.setId(userId);
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put(postTime, postId);
		line.setUserData(map);
		dao.insert(ColumnFamilyConfig.USER_LINE.toString(), line);
	}

	
	public void addUser(String userName, String email, String password) {
		User<String> user = new User<String>();
		dao.insert(ColumnFamilyConfig.USER.toString(), user);
		
	}
	
	public UserLine<String> getUserLine(String userId, int noOfRows) {
		UserLine<String> line = new UserLine<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.USER_LINE.toString(), userId, line, startTime, endTime, noOfRows);
		return line;
	}

	public Picture<String> getImage(String imageId) {
		Picture<String> mapping = new Picture<String>();
		dao.get(ColumnFamilyConfig.PICTURE.toString(), imageId, mapping);
		return mapping;
	}
	
	public Post<String> getPost(String postId) {
		Post<String> mapping = new Post<String>();
		dao.get(ColumnFamilyConfig.POST.toString(), postId, mapping);
		return mapping;
	}
	
	public void addTimelineUpdate(TimeLine<String> timeLine) {
		if (timeLine != null && (!timeLine.getTimeLineData().isEmpty())){
			dao.insert(ColumnFamilyConfig.TIME_LINE.toString(),timeLine);
		}
	}
	
	public TimeLine<String> getTimeLine(String userId, int noOfRows) {
		TimeLine<String> line = new TimeLine<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.TIME_LINE.toString(), userId, line, startTime, endTime, noOfRows);
		return line;
	}
	
	public User<String> getUser(String userId){
		User<String> user = new User<String>();
		dao.get(ColumnFamilyConfig.USER.toString(),userId,user);
		return user;
	}
	
	public void addFollower(Follower<String> follower){
		if (follower != null && (!follower.getFollowerMap().isEmpty())){
			dao.insert(ColumnFamilyConfig.FOLLOWER.toString(),follower);
		}		
	}
	
	public Follower<String> getFollower(String userId, int noOfRows) {
		Follower<String> follower = new Follower<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.FOLLOWER.toString(), userId, follower, startTime, endTime, noOfRows);
		return follower;
	}
	
	public void addFriends(Friend<String> friend){
		if (friend != null && (!friend.getFriendMap().isEmpty())){
			dao.insert(ColumnFamilyConfig.FRIEND.toString(),friend);
		}		
	}
	
	public Friend<String> getFriends(String userId, int noOfRows) {
		Friend<String> friend = new Friend<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.FRIEND.toString(), userId, friend, startTime, endTime, noOfRows);
		return friend;
	}
	
	public void addPostComments(PostCommentsMapping<String> postCommentsMapping){
		if (postCommentsMapping != null && (!postCommentsMapping.getMap().isEmpty())){
			dao.insert(ColumnFamilyConfig.POST_COMMENT_MAPPING.toString(),postCommentsMapping);
		}		
	}
	
	public PostCommentsMapping<String> getPostCommments(String postId, int noOfRows) {
		PostCommentsMapping<String> postCommentsMapping = new PostCommentsMapping<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.POST_COMMENT_MAPPING.toString(), postId, postCommentsMapping, startTime, endTime, noOfRows);
		return postCommentsMapping;
	}
	
	public void addPostCaptions(PostCaptionMapping<String> postCaptionMapping){
		if (postCaptionMapping != null && (!postCaptionMapping.getMap().isEmpty())){
			dao.insert(ColumnFamilyConfig.POST_CAPTION_MAPPING.toString(),postCaptionMapping);
		}		
	}
	
	public PostCaptionMapping<String> getPostCaptions(String postId, int noOfRows) {
		PostCaptionMapping<String> postCaptionMapping = new PostCaptionMapping<String>();
		String endTime = String.valueOf(System.nanoTime());
		String startTime = String.valueOf((System.nanoTime() - 600000000));
		dao.getWithRange(ColumnFamilyConfig.POST_CAPTION_MAPPING.toString(), postId, postCaptionMapping, startTime, endTime, noOfRows);
		return postCaptionMapping;
	}
	
	public void getAllPictures() {
		List<Picture<String>> mappings = new ArrayList<Picture<String>>();
//		CassandraDao.getAllRow(CassandraDao.PICTURE,mappings,100,Picture.class);
	}
	
}
